<template>
  <div class="setting">
    <el-form ref="form" :model="form" label-width="207px">
      <el-form-item label="启用发票">
        <el-radio-group v-model="form.status">
          <el-radio label="open">开启</el-radio>
          <el-radio label="close">关闭</el-radio>
        </el-radio-group>
        <p v-show="tip === 'close'" style="margin:0;color:#8a8a8a;">启用后，用户可在“账户中心”申请发票。</p>
      </el-form-item>
      <div v-if="form.status === 'open'">
        <el-form-item label="可提供发票类型">
          <el-checkbox-group v-model="form.type">
            <el-checkbox label="电子普通发票" />
            <el-checkbox label="纸质普通发票" />
            <el-checkbox label="增值税专用发票" />
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="开票时效">
          <el-radio-group v-model="form.period">
            <el-radio label="永久有效">永久有效</el-radio>
            <el-radio label="一年">一年</el-radio>
            <el-radio label="六个月">六个月</el-radio>
            <el-radio label="三个月">三个月</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="纸质发票邮费设置">
          <el-radio-group v-model="form.postage">
            <el-radio label="货到付款">货到付款</el-radio>
            <el-radio label="满额包邮">满额包邮</el-radio>
            <el-radio label="包邮">包邮</el-radio>
          </el-radio-group>
          <p v-if="form.postage === '满额包邮'" style="margin:0;font-size:12px;color:#8a8a8a;">未达到包邮金额则货到付款。</p>
        </el-form-item>
        <el-form-item v-if="form.postage === '满额包邮'" label="包邮金额">
          <el-input v-model="form.amount" style="width:400px;"/>
        </el-form-item>
        <el-form-item label="发票内容">
          <el-input :rows="6" v-model="form.content" type="textarea" style="width:400px;"/>
        </el-form-item>
      </div>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getInvoiceInstall, saveInvoiceInstall } from '@/api/deal/invoice' // 数据接口
import { getTenantId } from '@/utils/auth'
export default {
  components: {
  },
  data() {
    return {
      form: {
        status: 'close',
        period: '永久有效',
        postage: '货到付款',
        type: ['电子普通发票', '纸质普通发票'],
        amount: '0',
        content: '发票'
      },
      tip: 'close'
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      getInvoiceInstall(getTenantId()).then(response => {
        if (response.data.item) {
          this.form = response.data.item
          this.tip = response.data.item.status
          this.form.type = this.form.type.split(',')
        }
      })
    },
    onSubmit() {
      this.tip = this.form.status === 'close' ? 'close' : 'open'
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.type = this.form.type.toString()
          this.form.tenantId = getTenantId()
          saveInvoiceInstall(this.form).then(response => {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success',
              duration: 2000
            })
            this.form.type = this.form.type.split(',')
          })
        } else {
          this.$message.error('保存失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  margin-top: 0px;
  padding: 0;
}
</style>
